import ch.sfug.events.ErrorEvent;
import ch.sfug.events.Event;
import ch.sfug.net.loader.AbstractLoader;

import TextField.StyleSheet;

/**
 * @author loop
 *
 * a class that loads a css stylesheet
 */
class ch.sfug.net.loader.CSSLoader extends AbstractLoader {

	private var css:StyleSheet;

	/**
	 * constructor
	 * @param cssUrl the css url
	 * @param noCache (optional) if true the loaded url will contain a random number to prevent caching. default is false.
	 */
	public function CSSLoader( cssUrl:String, noCache:Boolean ) {
		super( cssUrl, noCache );
		css = new StyleSheet();
		var t:CSSLoader = this;
		css.onLoad = function( suc:Boolean ):Void {
			t.onLoad( suc );
		};
	}

	/**
	 * starts the download the css file
	 * @param cssUrl (optional) reassign url before loading
	 * @param noCache (optional) if true the loaded url will contain a random number to prevent caching. default is false.
	 */
	public function load( cssUrl:String, noCache:Boolean ):Void {
		url = cssUrl;
		preventCacheing = noCache;
		css.load( url );
	}

	/**
	 * returns the css file
	 */
	public function getResult(  ):StyleSheet {
		return css;
	}

	/**
	 * catch the event from the css to dispatch the complete event or errorevent
	 */
	private function onLoad( success:Boolean ):Void {
		if( success ) {
			dispatchEvent( new Event( Event.COMPLETE ) );
		} else {
			dispatchEvent( new ErrorEvent( ErrorEvent.ERROR, "could not load css file: " + _url ) );
		}
	}
}